<template>
  <div class="app">

    <nav class="navbar">
      <div class="container">
        <router-link to="/" class="navbar-brand">檀溪AI技术博客</router-link>
        <div class="nav-links">
          <router-link to="/" class="nav-link">首页</router-link>
          <router-link to="/blog" class="nav-link">博客</router-link>
          <router-link to="/ai-projects" class="nav-link">AI项目分享实践</router-link>
          <router-link to="/chat" class="nav-link">AI助手</router-link>
          <router-link to="/travel" class="nav-link">旅游博客</router-link>
          <router-link to="/about" class="nav-link">关于我</router-link>
          <router-link to="/contact" class="nav-link">联系我</router-link>
          
          <!-- 用户未登录时显示登录按钮 -->
          <template v-if="!userStore.isLoggedIn">
            <router-link to="/login" class="login-btn">登录/注册</router-link>
          </template>
          
          <!-- 用户已登录时显示用户信息 -->
          <div v-else class="user-info">
            <div class="user-avatar" @click="toggleUserMenu">
              <img :src="userAvatar" :alt="userStore.username" class="avatar-img">
            </div>
            <!-- 用户菜单 -->
            <div v-if="showUserMenu" class="user-menu">
              <div class="user-menu-header">
                <span class="username">{{ userStore.username }}</span>
              </div>
              <div class="user-menu-items">
                <router-link to="/write" class="menu-item">写文章</router-link>
                <a href="#" class="menu-item" @click.prevent="handleLogout">退出登录</a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </nav>

    <router-view v-slot="{ Component }">
      <component :is="Component" />
    </router-view>

    <footer class="footer">
      <div class="container">
        <p>&copy; 2025 檀溪AI的博客. All Rights Reserved.</p>
      </div>
    </footer>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useUserStore } from '@/store/user'
import { useRouter } from 'vue-router'

const router = useRouter()
const userStore = useUserStore()
const showUserMenu = ref(false)

// 修改计算属性的定义
const userAvatar = computed(() => {
  return userStore?.avatar || 'https://c-ssl.duitang.com/uploads/item/201804/21/20180421010211_srxpb.jpeg'
})

const toggleUserMenu = () => {
  showUserMenu.value = !showUserMenu.value
}

const handleLogout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('refreshToken')
  userStore.logout()
  router.push('/login')
  showUserMenu.value = false
}

// 点击页面其他地方关闭用户菜单
const closeUserMenu = (event) => {
  const userInfo = document.querySelector('.user-info')
  if (userInfo && !userInfo.contains(event.target)) {
    showUserMenu.value = false
  }
}

// 添加全局点击事件监听
window.addEventListener('click', closeUserMenu)

onMounted(() => {
  console.log('App组件已挂载')
})
</script>

<style>
.debug-info {
  padding: 10px;
  background-color: #f0f0f0;
  text-align: center;
}

.app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.navbar {
  padding: 1rem 0;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1rem;
}

.navbar .container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.navbar-brand {
  font-size: 1.5rem;
  font-weight: bold;
  color: #333;
  text-decoration: none;
}

.nav-links {
  display: flex;
  gap: 1.5rem;
  align-items: center;
}

.nav-link {
  color: #666;
  text-decoration: none;
  transition: color 0.3s;
}

.nav-link:hover {
  color: #333;
}

.user-info {
  position: relative;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  cursor: pointer;
  overflow: hidden;
  border: 2px solid #007bff;
  background-color: #f8f9fa;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.user-avatar:hover {
  border-color: #0056b3;
  transform: scale(1.05);
  box-shadow: 0 4px 8px rgba(0,0,0,0.15);
}

.avatar-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

.user-menu {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 0.5rem;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
  min-width: 220px;
  z-index: 1000;
  border: 1px solid #eaeaea;
  animation: slideDown 0.2s ease-out;
}

.user-menu-header {
  padding: 1rem;
  border-bottom: 1px solid #eee;
  background-color: #f8f9fa;
}

.username {
  font-weight: 600;
  color: #2c3e50;
  font-size: 1.1rem;
  display: block;
  margin-bottom: 0.25rem;
}

.user-menu-items {
  padding: 0.5rem 0;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 0.75rem 1rem;
  color: #4a5568;
  text-decoration: none;
  transition: all 0.2s ease;
  font-weight: 500;
}

.menu-item:hover {
  background-color: #f0f4f8;
  color: #2c3e50;
  padding-left: 1.25rem;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.footer {
  margin-top: auto;
  padding: 2rem 0;
  background-color: #f8f9fa;
  text-align: center;
  color: #666;
}
</style>